package com.caifeng.filetest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText name, pwd;
    Button button;
    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Map<String, String> userInfo = SPFQQ.getUserInfo(this);
        if (userInfo != null) {
            name.setText(userInfo.get("account"));
            pwd.setText(userInfo.get("password"));
        }
    }

    private void initView() {
        name = findViewById(R.id.username);
        pwd = findViewById(R.id.userpassword);
        button = findViewById(R.id.login);
        checkBox = findViewById(R.id.c_pwd);
        button.setOnClickListener(this);
        checkBox.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                String account = name.getText().toString().trim();
                String password = pwd.getText().toString().trim();
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(this, "没有账号输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "没有密码输入", Toast.LENGTH_SHORT).show();
                    return;

                }
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                //勾选记住密码
                if (checkBox.isChecked()) {
                    boolean isSaveSuccess = SPFQQ.saveUserInfo(this, account, password);
                    if (isSaveSuccess) {
                        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    //没有勾选记住密码
                    boolean isSaveSuccess = SPFQQ.saveUserInfo(this, account, " ");
                    if (isSaveSuccess) {
                        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                    }
                }

                break;
        }


    }

}
